import enum_data.FormStatus;
import enum_data.MenuType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Form_SingleData extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField txtFld;
    private JLabel lbl_NameField;
    private String userID;
    private final Object formMode;

    private int sizeWidth = 300;
    private int sizeHeight = 100;

    public Form_SingleData(String userID, FormStatus formMode) throws SQLException {

        setSize(sizeWidth, sizeHeight);
        setResizable(false);
        setContentPane(contentPane);
        setModal(true);
//        setAlwaysOnTop(true);

        this.userID = userID;
        this.formMode = formMode;

        ResultSet doljnostList = null;

        //Размещаем окно по центру экрана
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int locationX = (screenSize.width - sizeWidth) / 2;
        int locationY = (screenSize.height - sizeHeight) / 2;
        setBounds(locationX, locationY, getWidth(), getHeight());

        if (FormMain.getCurrentSubMenu() == MenuType.SPRAVOCHNIK_PRIVILEGIES && formMode == FormStatus.EDIT){
            doljnostList = TEDatabase.execQuery("SELECT privilegie_name FROM tradexpert.privilegie " +
                    "WHERE privilegie.id = '" + userID + "'" );

            doljnostList.next();
            lbl_NameField.setText("Должность:");
            txtFld.setText(doljnostList.getString("privilegie_name"));
        }

        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

// call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

// call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }



    private void onOK() {
        //Сохранение полей формы в БД
        String queryUpdate;

        if (formMode == FormStatus.NEW){
            queryUpdate = "INSERT INTO tradexpert.privilegie (privilegie_name) " +
                    "VALUES ('" + txtFld.getText() + "')";
        }else {
            queryUpdate = "UPDATE tradexpert.privilegie SET privilegie_name='" + txtFld.getText() + "' WHERE id='" + userID + "'";
        }
        try {
            TEDatabase.updateDB(queryUpdate);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Ошибка добавления должности - " + e.getMessage());
            buttonOK.setEnabled(false);
            return;
        }

        dispose();
    }

    private void onCancel() {
// add your code here if necessary
        dispose();
    }
}
